//
//  SelfTabbar.m
//  YouthHostel
//
//  Created by Lorain mac pro on 2016/9/27.
//  Copyright © 2016年 Lorain mac pro. All rights reserved.
//

#import "SelfTabbar.h"
#import "PrefixHeader.pch"
@interface SelfTabbar ()

@end

@implementation SelfTabbar

- (id)initWithImage:(NSString *)imagename initWithBackImage:(NSArray *)ImgArr andSelectImg:(NSArray *)selectimgArr andCount:(int)count andTitle:(NSArray *)titleArr{
    self = [super init];
    
    if (self) {
        
        [self hideSystemView];
        
        self.arr = ImgArr;
        
        self.arr1 = selectimgArr;

        self.x = count;
        
        if (imagename) {
            self.imagelabel.image = [UIImage imageNamed:imagename];
        }else{
            self.imagelabel.image = [UIImage imageNamed:@" "];
        }
        
        UILabel *mylable = [[UILabel alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49,WIDTH , 1)];
        
        mylable.backgroundColor = [UIColor whiteColor];
        
        [self.myview addSubview:mylable];
        
        self.myview = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 49,WIDTH , 49)];
        
        self.myview.backgroundColor = COLOR(242, 242, 242, 1);
        
        [self.view addSubview:_myview];
        
        
        for (int i = 0; i < count; i++) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(i * (SCREEN_WIDTH/count) + 27, 6, 30, 30)];
            
            UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(i * (SCREEN_WIDTH/count) + 27, 38, 30, 8)];
            
            titleLable.textColor = [UIColor blackColor];
            
            titleLable.text = titleArr[i];
            
            titleLable.textAlignment = NSTextAlignmentCenter;
            
            titleLable.font = [UIFont systemFontOfSize:10.0 weight:UIFontWeightThin];
            
            titleLable.tag = 160 + i;
            
            btn.tag = 150 + i;
            
            [self.myview addSubview:titleLable];
            [self.myview addSubview:btn];
            
            [btn addTarget:self action:@selector(go:) forControlEvents:UIControlEventTouchUpInside];
            
            if (i == 0) {
                [btn setBackgroundImage:[UIImage imageNamed:selectimgArr[i]] forState:0];
                titleLable.textColor = MainColor;
            }else{
                [btn setBackgroundImage:[UIImage imageNamed:ImgArr[i]]forState:0];
                titleLable.textColor = [UIColor blackColor];
            }
            
        }
       
        
    }
    
    return self;
}

- (void)hideSystemView{
    
    for (UIView *view in self.view.subviews) {
        if([view isKindOfClass:[self.tabBar class]])
            [view setHidden:YES];
    }
    
    
}


- (void)go:(UIButton *)sender{
    
    self.selectedIndex = sender.tag - 150;
    
    
    UIViewAnimationOptions options = UIViewAnimationCurveLinear | UIViewAnimationOptionAllowUserInteraction;
    
    [UIView animateWithDuration:0.2 delay:0.0 options:options animations:^
     {
         self.view.alpha = 1.0;
         
     } completion:nil];
    
    
    
    for (int i = 0; i < self.x; i++) {
        UIButton *tempbtn = [self.view viewWithTag:150 + i];
        UILabel *templable = [self.view viewWithTag:160 + i];
        
        if (tempbtn.tag == sender.tag) {
            [tempbtn setBackgroundImage:[UIImage imageNamed:self.arr1[i]] forState:0];
            templable.textColor = MainColor;
            
        }else{
            [tempbtn setBackgroundImage:[UIImage imageNamed:self.arr[i]] forState:0];
            templable.textColor = [UIColor blackColor];
            
        }
    }
}

//- (void)viewDidAppear:(BOOL)animated{
//    
//    self.myview.hidden = NO;
//    
//}
//
//- (void)setHidesBottomBarWhenPushed:(BOOL)hidesBottomBarWhenPushed{
//    
//    self.myview.hidden = hidesBottomBarWhenPushed;
//}

- (void)hideTabView{
    self.myview.hidden = YES;
}
- (void)showTabView{
    self.myview.hidden = NO;
}




@end

